TWB.New_Module({
	///////////////////////////
	name : "BackNext",		 //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	GetVil : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var _WN_=TWB._window();
		if(typeof _WN_.wrappedJSObject!="undefined"){
			_WN_=_WN_.wrappedJSObject;
		}
		return _WN_.game_data.village.id;
	},
	LoadVil : function(village){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var url=TWB.URL();
		if(url.match("village=")){
			url=url.replace(/village=[n]{0,1}\d+/,"village="+village);
		}
		else{
			url+="&village="+village;
		}
		TWB.Load_Url(url);
	},
	GetFilter : function(state){
		var A=TWB.gei("TWB-Filter-Normal");
		var B=TWB.gei("TWB-Filter-Offensive");
		var C=TWB.gei("TWB-Filter-Defensive");
		if(B.style.color=="blue"){
			if(state=='back'){
				return TWB.Modules.Filter.BackO();
			}
			else{
				return TWB.Modules.Filter.NextO();
			}
		}
		if(C.style.color=="blue"){
			if(state=='back'){
				return TWB.Modules.Filter.BackD();
			}
			else{
				return TWB.Modules.Filter.NextD();
			}
		}
		return true;
	},
	Back : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		if(TWB.Modules.BackNext.GetFilter('back')==true){
			var i;
			var V=TWB.gei("TWB-VillageList").childNodes;
			var cvil=TWB.Modules.BackNext.GetVil();
			for(i=0; i<V.length; i++){
				if(V[i].getAttribute("id").replace("TWB-v","")==cvil){
					break;
				}
			}
			if(i-1>=0){
				TWB.Modules.BackNext.LoadVil(V[i-1].getAttribute("id").replace("TWB-v",""));
			}
			else{
				alert(TWB.lang("startvil"));
			}
		}
	},
	Next : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		if(TWB.Modules.BackNext.GetFilter('next')){
			var i;
			var V=TWB.gei("TWB-VillageList").childNodes;
			var cvil=TWB.Modules.BackNext.GetVil();
			for(i=0; i<V.length; i++){
				if(V[i].getAttribute("id").replace("TWB-v","")==cvil){
					break;
				}
			}
			if(i-(-1)<V.length){
				TWB.Modules.BackNext.LoadVil(V[i-(-1)].getAttribute("id").replace("TWB-v",""));
			}
			else{
				alert(TWB.lang("endvil"));
			}
		}
	}
});